package com.freedom.pms.goods.domain.factory.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.common.core.base.IBaseEnum;
import com.freedom.common.core.enums.pms.GoodsPublishStatusEnum;
import com.freedom.common.core.enums.pms.GoodsVerifyStatusEnum;
import com.freedom.pms.goods.application.converter.GoodsSpuConverter;
import com.freedom.pms.goods.domain.factory.GoodsSpuFactory;
import com.freedom.pms.goods.domain.pojo.bo.GoodsSpuBO;
import com.freedom.pms.goods.domain.pojo.bo.ManageBrandBO;
import com.freedom.pms.goods.domain.pojo.bo.ManageCategoryBO;
import com.freedom.pms.goods.domain.repository.ManageBrandRepository;
import com.freedom.pms.goods.domain.repository.ManageCategoryRepository;
import com.freedom.pms.goods.infrastructure.persistence.pojo.vo.AdminGoodsSpuDetailVO;
import com.freedom.pms.goods.infrastructure.persistence.pojo.vo.AdminGoodsSpuPageVO;
import com.freedom.pms.supplier.domain.repository.SupplierStoreRepository;
import com.freedom.pms.supplier.infrastructure.persistence.pojo.bo.SupplierStoreBO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@RequiredArgsConstructor
public class GoodsSpuFactoryImpl implements GoodsSpuFactory {

    private final GoodsSpuConverter goodsSpuConverter;

    private final ManageBrandRepository manageBrandRepository;

    private final ManageCategoryRepository manageCategoryRepository;

    private final SupplierStoreRepository supplierStoreRepository;

    @Override
    public Page<AdminGoodsSpuPageVO> createAdminPageVO(Page<GoodsSpuBO> goodsSpuBOPage) {
        Page<AdminGoodsSpuPageVO> resultPage = goodsSpuConverter.toPageVO(goodsSpuBOPage);
        if (CollUtil.isNotEmpty(resultPage.getRecords())) {
            for (AdminGoodsSpuPageVO goodsSpu : resultPage.getRecords()) {
                ManageBrandBO manageBrandBO = manageBrandRepository.findById(goodsSpu.getBrandId());
                if (Objects.nonNull(manageBrandBO)) {
                    goodsSpu.setBrandName(manageBrandBO.getName());
                }
                ManageCategoryBO manageCategoryBO = manageCategoryRepository.findById(goodsSpu.getCategoryId());
                if (Objects.nonNull(manageCategoryBO)) {
                    goodsSpu.setCategoryName(manageCategoryBO.getName());
                }
                SupplierStoreBO supplierStoreBO = supplierStoreRepository.findById(goodsSpu.getStoreId());
                if (Objects.nonNull(supplierStoreBO)) {
                    goodsSpu.setStoreName(supplierStoreBO.getName());
                }
                goodsSpu.setPublishStatusDesc(IBaseEnum.getLabelByValue(goodsSpu.getPublishStatus() ,GoodsPublishStatusEnum.class));
                goodsSpu.setVerifyStatusDesc(IBaseEnum.getLabelByValue(goodsSpu.getVerifyStatus(), GoodsVerifyStatusEnum.class));
            }
        }
        return resultPage;
    }

    @Override
    public AdminGoodsSpuDetailVO createAdminGoodsSpuDetailO(GoodsSpuBO goodsSpuBO) {
        AdminGoodsSpuDetailVO adminGoodsSpuDetailVO = goodsSpuConverter.toDetailVO(goodsSpuBO);

        return adminGoodsSpuDetailVO;
    }
}
